import Vue from 'vue'
import VueRouter from 'vue-router'
// 导入登录组件
import Login from '@/components/login'
import home from '@/components/Home.vue'
// 导入欢迎组件
import Welcome from '@/components/Welcome.vue'
// 导入用户组件
import Users from '@/components/user/Users.vue'
import Rights from '@/components/power/Rights'
import Roles from '@/components/power/Roles'
import Cate from '@/components/goods/cate'
import Params from '@/components/goods/params'
import List from '@/components/goods/list'
import Add from '@/components/goods/add'
import Order from '@/components/order/order'
import Report from '@/components/report/report'
Vue.use(VueRouter)

const routes = [
    // 路由重定向
    { path: '/', redirect: '/login' },
    // 登录组件路由
    { path: '/login', component: Login },

    {
        path: '/home',
        component: home,
        redirect: '/welcome',
        children: [
            { path: '/welcome', component: Welcome },
            { path: '/users', component: Users },
            { path: '/rights', component: Rights },
            { path: '/roles', component: Roles },
            { path: '/categories', component: Cate },
            { path: '/params', component: Params },
            { path: '/goods', component: List },
            { path: '/goods/add', component: Add },
            { path: '/orders', component: Order },
            { path: '/reports', component: Report }
        ]
    }

]



const router = new VueRouter({
        routes
    })
    // 路由首位导航 控制页面访问权限
router.beforeEach((to, from, next) => {
    //  to 去的页面 from来自页面 next到的页面
    if (to.path == '/login') {
        return next()
    }
    // 获取token
    const token = window.sessionStorage.getItem('token')
    if (!token)
        return next('/login')
    next()
})



export default router